#include <bits/stdc++.h>

using namespace std;

struct Node
{
public:
  int val;
  Node *l;
  Node *r;
  Node(int val_ = 0, Node *l_ = nullptr, Node *r_ = nullptr)
      : val(val_), l(l_), r(r_)
  {
    ;
  }
  /* data */
};
typedef Node *Tree;

class Solution
{
public:
  vector<int> pre;
  vector<int> inorder;
  int pre_idx;
  unordered_map<int, int> inorder_idx_map;
  Tree root;
  Solution(vector<int> &pre_, vector<int> &inorder_) : pre(pre_), inorder(inorder_)
  {
    pre_idx = 0;
    int i = 0;
    for (auto num : inorder)
    {
      inorder_idx_map[num] = i++;
    }
    this->buildTree();
  }
  Tree helper(int l, int r)
  {
    if (l > r || pre_idx >= (int)pre.size())
    {
      return nullptr;
    }
    int rootVal = pre[pre_idx];
    int in_idx = inorder_idx_map[rootVal];
    Tree thisroot = new Node(rootVal);
    pre_idx++;
    thisroot->l = helper(l, in_idx - 1);
    thisroot->r = helper(in_idx + 1, r);
    return thisroot;
  }
  void buildTree()
  {
    this->root = helper(0, inorder.size() - 1);
  }

  void Postprint()
  {
    helpPostPrint(this->root);
    cout << endl;
  }
  void helpPostPrint(Node *node)
  {
    if (node == nullptr)
      return;
    helpPostPrint(node->l);
    helpPostPrint(node->r);
    cout << node->val << " ";
  }
};

int main()
{
  vector<int> pre;
  vector<int> inorder;
  string readed;

  getline(cin, readed);
  stringstream thisline(readed);
  int num;
  while (thisline >> num)
  {
    pre.push_back(num);
  }

  string readed2;
  getline(cin, readed2);
  stringstream thisline2(readed2);
  int num2;
  while (thisline2 >> num2)
  {
    inorder.push_back(num2);
  }

  Solution s(pre, inorder);
  s.Postprint();
}